package color

import (
	"fmt"
	"strings"
)

// Color 颜色类型
type Color string

const (
	Reset Color = "\033[0m" // 颜色重置

	Empty Color = "" // 无色

	RedText    Color = "\033[8;31m" // 红色
	GreenText  Color = "\033[8;32m" // 绿色
	YellowText Color = "\033[8;33m" // 黄色
	BlueText   Color = "\033[8;34m" // 蓝色
	PurpleText Color = "\033[8;35m" // 紫色
	CyanText   Color = "\033[8;36m" // 蓝绿色
	WhiteText  Color = "\033[8;37m" // 白色

	RedTextHighlight    Color = "\033[8;91m"
	GreenTextHighlight  Color = "\033[8;92m"
	YellowTextHighlight Color = "\033[8;93m"
	BlueTextHighlight   Color = "\033[8;94m"
	PurpleTextHighlight Color = "\033[8;95m"
	CyanTextHighlight   Color = "\033[8;96m"
	WhiteTextHighlight  Color = "\033[8;97m"

	RedBackground    Color = "\033[8;97;41m"
	GreenBackground  Color = "\033[8;97;42m"
	YellowBackground Color = "\033[8;90;43m"
	BlueBackground   Color = "\033[8;97;44m"
	PurpleBackground Color = "\033[8;97;45m"
	CyanBackground   Color = "\033[8;97;46m"
	WhiteBackground  Color = "\033[8;90;47m"
)

// Parse 将字符串 s 转换为有颜色的 s
func (my Color) Parse(s string) string {
	return Parse(my, s)
}

func (my Color) Valid() bool {
	return my != Empty && my != Reset
}

func (my Color) String() string {
	return string(my)
}

// Parse 将字符串 s 转换为有颜色的 s
func Parse(c Color, s string) string {
	if !c.Valid() {
		return s
	}
	// 因为颜色遇到换行就失效了，这里处理下
	s = strings.ReplaceAll(s, "\n", string(Reset)+"\n"+string(c))
	s = strings.ReplaceAll(s, string(c)+string(Reset), string(Empty))
	return fmt.Sprintf("%s%s%s", c, s, Reset)
}
